home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-04-09 | 4.1 KB | 158 lines | [TEXT/KEEN] |
- #$Print_STR#: as loaded by ReadResource
-
- #The approach below uses "input on demand" to allow us
- #to step through the bytes in the resource. Each call to
- #GetNextByte or PrintCharAndAdvance retrieves the next byte, grabbing
- #the next input line if necessary. The variable bytePos is
- #automatically advanced whenever a byte is read, and the calculation
- #of the field number and position within the field is hidden within
- #GetNextByte. To skip over n bytes, add n to bytePos.
-
- #Format of STR# resource:
- #2 number of strings (numStrings below)
- #--repeated for numStrings:
- #1 length of string (stringLen below)
- #n string text, stringLen bytes
-
- #Format of resource as retrieved by Read Resource:
- #byteNumber: 4bytes 4bytes 4bytes 4bytes ascii version of the bytes
- #the last line may be padded with zeroes, for example
- # 240: 07556E6D 61726BC9 00000000 09417574 .Unmark......Aut
- # 256: 6F6D6172 6BC90000 00000000 00000000 omark......
-
- #bytePos ranges 0 : up. It is automatically
- #advanced when a byte is read by GetNextByte(). It can be
- #manually advanced to skip unwanted bytes.
-
- # User’s Manual references:
- # «hAWK User’s Manual» «F Running hAWK programs»
- # «hAWK User’s Manual» «L 5 Regular expressions»
- # «hAWK User’s Manual» «M 5 Built-in string and file functions»
- # «hAWK User’s Manual» «K 4 Built-in variables»
- # «hAWK User’s Manual» «K 8 Arrays»
- # «hAWK User’s Manual» «N User-defined functions»
- # «hAWK User’s Manual» «P 3 The getline function»
- # «hAWK User’s Manual» «O 3 Output into files»
- # «hAWK User’s Manual» «Q The hAWK function»
-
- FNR == 1 {
- numStrings = GetNextBytesAsNumber(2)
- print "Number of strings:", numStrings;
- while (1)
- {
- stringLen = GetNextByte()
- printf("%3d ", stringLen);
- while (stringLen > 0)
- {
- PrintCharAndAdvance();
- --stringLen;
- }
- print ""
- }
- }
-
-
-
- function PrintCharAndAdvance()
- {
- printf("%c", GetNextByte());
- }
-
- #Convert ASCII representation of next byte, eg '1F', to a single
- #number, eg 31.
- function GetNextByte( lineByte, field, char, pos)
- {
- #Convert byte position in resource to field and position in field.
- lineByte = bytePos - (FNR - 1) * 16;
- field = int((lineByte)/4);
- while (field > 3)#the next line of input is wanted
- {
- if ((getline) <= 0)
- {
- print ""; print "End of file."; exit;
- }
- lineByte = bytePos - (FNR - 1) * 16;
- field = int((lineByte)/4);
- }
- pos = lineByte - field * 4;
- field += 2;
- pos = 2 * pos + 1;
- char = HexToDec(substr($field, pos, 2));
- ++bytePos;
- return char
- }
-
- #Return raw ASCII representation of byte, eg "2F".
- function GetNextRawByte( lineByte, field, char, pos)
- {
- #Convert byte position in resource to field and position in field.
- lineByte = bytePos - (FNR - 1) * 16;
- field = int((lineByte)/4);
- while (field > 3)#the next line of input is wanted
- {
- if ((getline) <= 0)
- {
- print ""; print "End of file."; exit;
- }
- lineByte = bytePos - (FNR - 1) * 16;
- field = int((lineByte)/4);
- }
- pos = lineByte - field * 4;
- field += 2;
- pos = 2 * pos + 1;
- ++bytePos;
- return substr($field, pos, 2);
- }
-
- #Convert ASCII representation of 1, 2, or 4 bytes to a single
- #unsigned number.
- function GetNextBytesAsNumber(howManyBytes, i, temp, numString, number)
- {
- for (i = 1; i <= howManyBytes; ++i)
- {
- temp = GetNextRawByte();
- numString = numString temp;
- }
- number = HexToDec(numString);
- return number;
- }
-
- #General purpose ASCII hex to number conversion.
- function HexToDec(h, len, nyb, hexDigit, power, d)
- {
- d = 0;
- len = length(h);
- power = 1
- for (nyb = len; nyb >= 1; --nyb)
- {
- hexDigit = substr(h, nyb, 1);
- d += DecimalEquiv(hexDigit) * power;
- power *= 16;
- }
- return d
- }
-
- #Convert one hex "digit" to the equivalent number.
- function DecimalEquiv(hd, dd)
- {
- dd = 0;
- if (match(hd,/[0-9]/) > 0)
- dd = hd + 0;#conversion from string to number is implicit
- else if (match(hd, /[a-fA-F]/) > 0)
- {
- if (match(hd, /[aA]/) > 0)
- dd = 10;
- else if (match(hd, /[bB]/) > 0)
- dd = 11;
- else if (match(hd, /[cC]/) > 0)
- dd = 12;
- else if (match(hd, /[dD]/) > 0)
- dd = 13;
- else if (match(hd, /[eE]/) > 0)
- dd = 14;
- else if (match(hd, /[fF]/) > 0)
- dd = 15;
- }
- return dd
- }
-